﻿using System;
using UnityEngine;

// Token: 0x020002DF RID: 735
public class TerrainScript : MonoBehaviour
{
	// Token: 0x06001108 RID: 4360 RVA: 0x0007DEF0 File Offset: 0x0007C0F0
	private void Start()
	{
		this.mainCamera = Camera.main;
		this.cameraTransform = this.mainCamera.transform;
		this.skybox = Resources.Load<Material>("Shaders/IslandSky");
		this.ambientAudioSource = base.gameObject.AddComponent<AudioSource>();
		this.ambientAudioSource.clip = SoundManager.This.natureSound;
		this.ambientAudioSource.loop = true;
		this.ambientAudioSource.outputAudioMixerGroup = SoundManager.mg_background;
		this.ambientAudioSource.dopplerLevel = 0f;
		this.ambientAudioSource.Play();
	}

	// Token: 0x06001109 RID: 4361 RVA: 0x0007DF86 File Offset: 0x0007C186
	private void Update()
	{
		this.UpdateAtmosphere();
		this.UpdateSounds();
	}

	// Token: 0x0600110A RID: 4362 RVA: 0x0007DF94 File Offset: 0x0007C194
	private void UpdateAtmosphere()
	{
		float value = 1f - CenterOrigin.WorldToVirtual(this.cameraTransform.position).y / 50000f;
		value = Mathf.Clamp(value, 0.5f, 1f);
		this.skybox.SetFloat("_AtmosphereThickness", value);
	}

	// Token: 0x0600110B RID: 4363 RVA: 0x0007DFE8 File Offset: 0x0007C1E8
	private void UpdateSounds()
	{
		Vector3 position = this.cameraTransform.position;
		RaycastHit raycastHit;
		bool flag = Physics.Raycast(this.cameraTransform.position, Vector3.down, out raycastHit, 10000f, Layers.mapMask);
		if (flag)
		{
			float y = CenterOrigin.WorldToVirtual(raycastHit.point).y;
			float num = raycastHit.distance;
			if (y < this.seaLevel)
			{
				y = this.seaLevel;
				float y2 = CenterOrigin.WorldToVirtual(position).y;
				if (y2 > this.seaLevel)
				{
					num = y2 - this.seaLevel;
				}
				else
				{
					num = 0f;
				}
			}
			float num2 = num + y;
			float num3 = 1f - Mathf.Clamp(num2 / this.windLevel, 0f, 1f);
			float num4 = 1f - Mathf.Clamp(num / 100f, 0f, 1f);
			float num5 = num3 * num4;
			if (num5 < 0.01f)
			{
				float num6 = Mathf.Min(this.windLevel, y + 100f);
				num5 = Mathf.Clamp01((position.y - num6) / 500f);
				this.SetAmbientClip(SoundManager.This.windSound);
			}
			else if (y == this.seaLevel)
			{
				this.SetAmbientClip(SoundManager.This.seaSound);
			}
			else
			{
				this.SetAmbientClip(SoundManager.This.natureSound);
			}
			this.ambientAudioSource.volume = num5;
		}
	}

	// Token: 0x0600110C RID: 4364 RVA: 0x0007E16B File Offset: 0x0007C36B
	private void SetAmbientClip(AudioClip clip)
	{
		SoundManager.SetSoundClip(this.ambientAudioSource, clip);
	}

	// Token: 0x04001667 RID: 5735
	private AudioSource ambientAudioSource;

	// Token: 0x04001668 RID: 5736
	private Camera mainCamera;

	// Token: 0x04001669 RID: 5737
	private float windLevel = 1000f;

	// Token: 0x0400166A RID: 5738
	private Material skybox;

	// Token: 0x0400166B RID: 5739
	private Transform cameraTransform;

	// Token: 0x0400166C RID: 5740
	private float seaLevel = 205f;
}
